package com.hyt.it.ogt.kq.service.gov.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.kq.common.gov.secmodule.SecModuleUtil;
import com.hyt.it.ogt.kq.service.gov.mapper.TimeRoomPasswordMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.TimeRoomPassword;
import com.hyt.it.ogt.kq.service.gov.model.param.TimeRoomPasswordParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.TimeRoomCodeVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.TimeRoomPasswordVO;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomPasswordService;
import com.hyt.model.PageParam;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 场次密码信息表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-04-08
 */
@Slf4j
@Service
public class TimeRoomPasswordServiceImpl extends BaseServiceImpl<TimeRoomPasswordMapper, TimeRoomPassword> implements ITimeRoomPasswordService {

    /**
     * 初始化场次密码数据
     * @param taskId
     * @param timeId
     */
    @Override
    public void initTimeRoomPassword(String taskId, String timeId) {

        //清除无效的场次密码
        baseMapper.deleteTimeRoomPassword(taskId, timeId);

        //查询需要生成密码的场次数据
        List<TimeRoomCodeVO> timeRoomList = baseMapper.selectTimeRoomNeedPassword(taskId, timeId);

        if (timeRoomList == null || timeRoomList.isEmpty()) {
            return;
        }

        List<TimeRoomPassword> timeRoomPasswords = new ArrayList<>();
        timeRoomList.stream().forEach(timeRoom -> {

            String key = StringUtils.join(timeRoom.getPlaceCode(),timeRoom.getTimeCode());

            TimeRoomPassword timeRoomPassword = new TimeRoomPassword();
            timeRoomPassword.setTaskId(taskId)
                    .setTimeId(timeId)
                    .setRoomId(timeRoom.getRoomId())
                    .setTimeRoomId(timeRoom.getId())
                    .setTenPassword(SecModuleUtil.genExamPass(key,1,10))
                    .setThirtyPassword(SecModuleUtil.genExamPass(key,1,30))
                    .setFullPassword(SecModuleUtil.genExamPass(key,2,0))
                    .setLatePassword(SecModuleUtil.genExamPass(key,3,1))
                    .setSinglePassword(SecModuleUtil.genExamPass(key,4,0))
                    .setSpecialPassword(SecModuleUtil.genExamPass(key,3,1))
                    .setId(UUIDUtils.newSortUUID());
            timeRoomPasswords.add(timeRoomPassword);
        });

        super.saveBatch(timeRoomPasswords);

    }

    /**
     * 获取场次密码
     * @param timeRoomPasswordParam
     * @return
     */
    @Override
    public Page<TimeRoomPasswordVO> page(PageParam<TimeRoomPasswordVO> pageParam, TimeRoomPasswordParam timeRoomPasswordParam) {
        return baseMapper.getTimeRoomPassword(pageParam,timeRoomPasswordParam);
    }

    @Override
    public List<TimeRoomPassword> getByTaskId(String taskId) {
        QueryWrapper<TimeRoomPassword> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TimeRoomPassword::getTaskId, taskId)
                .eq(TimeRoomPassword::getDelFlag, false);
        return list(queryWrapper);
    }

}
